const query = require('../models');


module.exports = {
    
    all: function (req, res) {
        
        const sql = `
        SELECT t.teacher_id teacherId, t.teacher_name teacherName, s.section_name sectionName, sex.sex_name teacherSex, e.education_name educationName, t.teacher_tel teacherTel, t.teacher_email teacherEmail, t.teacher_entry teacherEntry, t.teacher_standing teacherStanding 
        FROM teachers t 
        JOIN section s ON t.section_id = s.section_id
        JOIN education e ON t.education_id = e.education_id
        JOIN teachersex sex ON t.sex_id = sex.sex_id
        WHERE is_del=1
        order by t.teacher_id desc`;

        query(sql, null)
            .then(function (doc) {
                res.json({
                    code: '200',
                    msg: '操作成功',
                    data: doc
                })
            })
            .catch(function (err) {
                res.json({
                    code: '1002',
                    msg: '操作失败',
                    err
                })
            })
    },

    del: function(req,res) {
        const sql = `UPDATE teachers SET is_del = 0 WHERE teacher_id = ?`;
        
        let teachersId = req.body.teacherIds;
        
        
            query(sql,[teachersId])
            .then(function(doc) {
                res.json({
                    code: '200',
                    msg: '删除成功',
                    data: doc
                })
            })
            .catch(function(err) {
                res.json({
                    code: '1002',
                    msg: '删除失败',
                    err
                })
            })     
    },

    add: function (req, res) {
        //解构
        // let{
        //     teacherName,
        //     teacherTel,
        //     teacherEmail,
        //     teacherEntry,
        //     teacherStanding,
        //     teacherSection,
        //     teacherEducation,
        //     teacherSex
        //     } = req.body;
        console.log('新增请求参数:',req.body);
        let teacherName = req.body.teacherName;
        let teacherTel = req.body.teacherTel;
        let sectionId = req.body.sectionId;
        let teacherStanding = req.body.teacherStanding;
        let teacherEmail = req.body.teacherEmail;
        let sexId = req.body.sexId;
        let educationId = req.body.educationId;
        let teacherEntry = req.body.teacherEntry;
        // console.log(typeof sexId, educationId ,teacherEntry);

        const sql = `INSERT INTO teachers VALUES (NULL, ?, ?, ?,?,?,1,?,?,?)`;
        
        console.log('转化后：',[teacherName,teacherTel,teacherEmail,teacherEntry,teacherStanding,sectionId,educationId,sexId]);

        query(sql, [teacherName,teacherTel,teacherEmail,teacherEntry,teacherStanding,sectionId,educationId,sexId])
        .then (function (doc){
            console.log('新增后结果：',doc);
            res.json({
                code: '200',
                msg: '操作成功'
            })
        })
        .catch(function(err){
            
            res.json({
                code: '1002',
                msg: '操作失败',
                err
            })
        })
    },

    edit: function (req, res) {
        console.log('编辑请求参数:',req.body);
        let teacherName = req.body.teacherName;
        let teacherTel = req.body.teacherTel;
        let sectionId = req.body.sectionId;
        let teacherStanding = req.body.teacherStanding;
        let teacherEmail = req.body.teacherEmail;
        let sexId = req.body.sexId;
        let educationId = req.body.educationId;
        let teacherEntry = req.body.teacherEntry;
        let teacherId = req.body.teacherId;
        // console.log(typeof sexId, educationId ,teacherEntry);

        const sql = `
        UPDATE teachers 
        SET teacher_name = ?, teacher_tel = ?, teacher_email = ?, teacher_entry = ?, teacher_standing = ?, section_id = ?, education_id = ?, sex_id = ? 
        WHERE teacher_id = ?;`;
        
        console.log('转化后：',[teacherName,teacherTel,teacherEmail,teacherEntry,teacherStanding,sectionId,educationId,sexId,teacherId]);

        query(sql, [teacherName,teacherTel,teacherEmail,teacherEntry,teacherStanding,sectionId,educationId,sexId,teacherId])
        .then (function (doc){
            console.log('新增后结果：',doc);
            res.json({
                code: '200',
                msg: '操作成功'
            })
        })
        .catch(function(err){
            
            res.json({
                code: '1002',
                msg: '操作失败',
                err
            })
        })
    },

    getTeacher: function (req, res) {
        console.log('新增请求参数：', req.body);
        const teacherId = req.body.teacherId;

        const sql = `
        SELECT t.teacher_id teacherId, t.teacher_name teacherName, t.teacher_tel teacherTel, t.teacher_email teacherEmail, t.teacher_entry teacherEntry, t.teacher_standing teacherStanding, s.section_id sectionId, e.education_name educationName, sex.sex_name sexName 
        FROM teachers t
        join section s on t.section_id = s.section_id
        JOIN education e ON t.education_id = e.education_id
        JOIN teachersex sex ON t.sex_id = sex.sex_id
        WHERE teacher_id = ?`;

        query(sql, [teacherId])
        .then(function (doc) {
            console.log('查询某一个学生的结果：', doc);

            res.json({
            code: '200',
            msg: '操作成功',
            data: doc[0]
            });
        })
        .catch(function (err) {
            res.json({
            code: '1002',
            msg: '操作失败',
            err
            });
        });
    },

    getOneTeacher: function (req, res) {
        console.log('新增请求参数：', req.body);
        const teacherId = req.body.teacherId;

        const sql = `
        SELECT t.teacher_id teacherId, t.teacher_name teacherName, t.teacher_tel teacherTel, t.teacher_email teacherEmail, t.teacher_entry teacherEntry, t.teacher_standing teacherStanding, s.section_id sectionId, e.education_id educationId, sex.sex_id sexId 
        FROM teachers t
        join section s on t.section_id = s.section_id
        JOIN education e ON t.education_id = e.education_id
        JOIN teachersex sex ON t.sex_id = sex.sex_id
        WHERE teacher_id = ?`;

        query(sql, [teacherId])
        .then(function (doc) {
            console.log('查询某一个学生的结果：', doc);

            res.json({
            code: '200',
            msg: '操作成功',
            data: doc[0]
            });
        })
        .catch(function (err) {
            res.json({
            code: '1002',
            msg: '操作失败',
            err
            });
        });
    }

};
